<?php
namespace app\first\controller;

use app\first\model\DreamComments;
use app\first\model\Keywords;
use service\Category;
use think\db\Where;

/**
 * Class Article
 * @title 梦想加相关
 * @url /v1/dream
 * @desc  获取梦想加相关
 * @version 1.0
 * @package app\first\dream
 * @route('v1/dream')
 */
class Dream extends Base{
    //是否开启授权认证,默认开启
    public $apiAuth = false;
    protected $model = null;
    protected $validate = null;
    /*
    * 需要附加的其他方法,比如:topList,newList
    * protected $extraActionList = ['topList','newList',...];
    * 已经对核心的类进行了重写,扩展的新方法会自动添加到认证中,无需再手动书写
    */
    protected $extraActionList = [];

    protected $skipAuthActionList= [];

    public function __construct(){
        parent::__construct();
        $this->model = new \app\first\model\Dream();
        $this->validate = new \app\first\validate\Dream();
    }

    /**
     * @title 获取类目列表
     * @method index
     * @param string $city_id 城市id true
     * @param int $is_top 是否置顶 false 0 1是,0否
     * @param int $limit 每页条数 true 8
     * @param int $page 当前页数 true 1
     * @route('v1/dream/list','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function index($city_id='',$is_top=0,$limit=8,$page = 1){
        $map = [['status','eq',2]];
        if($city_id){
            array_push($map,['city_id','eq',$city_id]);
        }
        $order= $is_top ? "is_top desc,create_time desc":'create_time desc';
        $list = $this->model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取详情
     * @method read
     * @param int $id id true
     * @route('v1/dream/read','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id=0){
        $info = $this->model->getOneById($id);
        $info->hidden(['update_time','sort']);
        return $this->sendSuccess($info);
    }

    /**
     * @title 获取随机相关
     * @method like
     * @param int $cate_id 栏目id true 17
     * @param int $limit 条数 true 4
     * @route('v1/dream/like','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function like($cate_id=0,$limit=4){
        $article = new \app\first\model\Dream();
        $list = $article->likes($cate_id,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取最新发布
     * @method last_publish
     * @param int $city_id 城市id true
     * @param int $limit 条数 false 10
     * @route('v1/dream/last_publish','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function last_publish($city_id=0,$limit=10){
        $article = new \app\first\model\Dream();
        $list = $article->lastList($city_id,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 搜索
     * @method search
     * @param string $q 关键词 true
     * @param int $limit 条数 true 10
     * @param int $page 页数 true 1
     * @route('v1/dream/search','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function search($q='',$limit=10,$page=1){
        $article = new \app\first\model\Dream();
        if(!$q){
            return $this->sendError(0,'请输入关键词');
        }
        $where = new Where();
        $where['status']=2;
        $keywords = new Keywords();
        $keywords->addNew($q,2);
        $list = $article->search($q,$where,$limit);
        return $this->sendSuccess($list,'');
    }

    /**
     * @title 获取热门关键词
     * @method hot_keywords
     * @param int $limit 条数 false 10
     * @route('v1/dream/hot_keywords','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function hot_keywords($limit=10){
        $keywords = new Keywords();
        $list = $keywords
            ->field('id,keywords')
            ->where('type','eq',2)
            ->limit($limit)
            ->order('hits desc,create_time desc')
            ->select();
        return $this->sendSuccess($list);
    }

    /**
     * @title 评论
     * @method comments
     * @param int $id 梦想加ID true
     * @param string $order 排序 false desc asc正序,desc倒序
     * @route('v1/dream/comments','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function comments($id=0,$order='desc'){
        $article = new DreamComments();
        $where[] = ['aid','eq',$id];
        $list = $article->getAllList($where,"create_time {$order}");
//        $list = Category::unlimitedForLevel2($list->toArray(),0,'children','flow_id');
        return $this->sendSuccess($list);
    }
}